package com.atguigui.leetcode1;

import java.util.Arrays;

/**
 * 2389.和有限的最长子序列
 * Project: leetcode
 * Package: com.atguigui.leetcode1
 * Version: 1.0
 * <p>
 * Created by WJX on 2023/3/17 11:01
 */
public class P2389LongestSubsequenceWithLimitedSum {

    public static void main(String[] args) {
        Solution solution = new P2389LongestSubsequenceWithLimitedSum().new Solution();
        // TO TEST
    }

    class Solution {
        public int[] answerQueries(int[] nums, int[] queries) {

            //排序
            Arrays.sort(nums);

            int n = nums.length;
            int m = queries.length;
            for (int i = 0; i < m; i++) {
                //没有等于0
                int num = queries[i];
                int a = 0;
                while (a < n && num >= 0 && num >= nums[a]) {
                    num -= nums[a];
                    ++a;
                }

                queries[i] = a;
            }


            return queries;
        }


        public int[] answerQueries1(int[] nums, int[] queries) {

            //排序
            Arrays.sort(nums);

            int n = nums.length;
            int m = queries.length;
            for (int i = 0; i < m; i++) {
                //没有等于0
                int num = queries[i];
                int a = 0;
                for (int j = 0; j < n; j++) {
                    if (num >= nums[j]) {
                        num -= nums[j];
                        a = j + 1;
                    } else {
                        break;
                    }
                }

                queries[i] = a;
            }


            return queries;
        }


        public int[] answerQueries2(int[] nums, int[] queries) {

            //排序
            Arrays.sort(nums);

            int n = nums.length;
            int m = queries.length;
            for (int i = 0; i < m; i++) {
                //没有等于0
                int num = queries[i];
                int a = 0;
                while (a < n && num >= nums[a] && num >= 0) {
                    num -= nums[a];
                    a++;
                }

                queries[i] = a;
            }


            return queries;
        }
    }
}
